package com.examsage.examsage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.examsage.examsage.model.Course;
import com.examsage.examsage.model.ExamSession;
import com.examsage.examsage.mapper.ExamSessionMapper;
import com.examsage.examsage.model.ExamSession;
import com.examsage.examsage.model.vo.ResponseVO;
import com.examsage.examsage.service.IExamSessionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author examsage
 * @since 2025-07-02
 */
@Service
public class ExamSessionServiceImpl extends ServiceImpl<ExamSessionMapper, ExamSession> implements IExamSessionService {

    @Resource
    private ExamSessionMapper examSessionMapper;

    @Override
    public ResponseVO addExamSession(ExamSession examSession){
        examSession.setGmtCreate(LocalDateTime.now());
        examSession.setGmtModified(LocalDateTime.now());
        ResponseVO responseVO = new ResponseVO();
        if (examSessionMapper.insert(examSession) > 0){
            responseVO.setStatusCode(200);
            responseVO.setMessage("添加成功");
            responseVO.setData(examSession);
        } else {
            responseVO.setStatusCode(500);
            responseVO.setMessage("添加失败");
            responseVO.setData(null);
        }
        return responseVO;
    }

    @Override
    public ResponseVO getExamSession(int year) {
        // 使用 QueryWrapper 构建查询条件，匹配 exam_year 字段
        List<ExamSession> examSessions = baseMapper.selectList(new QueryWrapper<ExamSession>()
                .eq("exam_year", year));

        ResponseVO responseVO = new ResponseVO();

        if (examSessions == null || examSessions.isEmpty()) {
            responseVO.setStatusCode(404);
            responseVO.setMessage("没有该年份");
            responseVO.setData(null);
        } else {
            responseVO.setStatusCode(200);
            responseVO.setMessage("查询成功");
            responseVO.setData(examSessions);
        }

        return responseVO;
    }



    @Override
    public ResponseVO getExamSessionById(Long id){
        ExamSession examSession = examSessionMapper.selectById(id);
        ResponseVO  responseVO = new ResponseVO();
        if (examSession != null){
            responseVO.setStatusCode(200);
            responseVO.setMessage("查询成功");
            responseVO.setData(examSession);
        } else {
            responseVO.setStatusCode(500);
            responseVO.setMessage("查询失败");
            responseVO.setData(null);
        }
        return responseVO;
    }

    @Override
    public ResponseVO updateExamSessionById(Long id, ExamSession examSession){
        UpdateWrapper<ExamSession> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);

        if (examSession.getSessionName() != null && !examSession.getSessionName().trim().isEmpty()) {
            updateWrapper.set("session_name", examSession.getSessionName());
        }
        if (examSession.getSessionCode() != null && !examSession.getSessionCode().trim().isEmpty()) {
            updateWrapper.set("session_code", examSession.getSessionCode());
        }

        // 自动填充 gmt_modified（如果已配置 MetaObjectHandler）
        ResponseVO responseVO = new ResponseVO();
        if (examSessionMapper.update(null, updateWrapper) > 0) {
            responseVO.setStatusCode(200);
            responseVO.setMessage("修改专业计划成功");
            responseVO.setData(examSessionMapper.selectById(id));
        } else {
            responseVO.setStatusCode(500);
            responseVO.setMessage("修改专业计划失败");
            responseVO.setData(null);
        }
        return responseVO;
    }

    @Override
    public ResponseVO deleteExamSessionById(Long id){
        ResponseVO responseVO = new ResponseVO();
        if (examSessionMapper.deleteById(id) == 1){
            responseVO.setStatusCode(200);
            responseVO.setMessage("删除成功");
            responseVO.setData(null);
        } else {
            responseVO.setStatusCode(500);
            responseVO.setMessage("删除失败");
            responseVO.setData(null);
        }
        return responseVO;
    }
}
